package com.weixinli.web.actions;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.weixinli.beans.DetailInfo;
import com.weixinli.beans.Mood;
import com.weixinli.beans.UserComment;
import com.weixinli.beans.UserInfo;
import com.weixinli.beans.UserReply;
import com.weixinli.biz.IMoodBiz;
import com.weixinli.biz.IUserCommentBiz;
import com.weixinli.biz.IUserReplyBiz;
import com.weixinli.utils.SessionAttributeName;
import com.weixinli.web.actions.model.JsonObject;

@Scope("prototype")
@Controller("moodsAction")
public class MoodsAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	@Resource(name="iMoodBiz")
	private IMoodBiz moodBiz;
	@Resource(name="iUserCommentBiz")
	private IUserCommentBiz userCommentBiz;
	@Resource(name="iUserReplyBiz")
	private IUserReplyBiz userReplyBiz;
	@Resource(name="jsonObject")
	private JsonObject jsonObject;
	
	private UserInfo userInfo;
	private Mood mood;
	private UserComment comment;
	private UserReply reply;
	private HttpSession session;
	private String code;
	
	public String moodData() {
		List<Mood> results = moodBiz.getTotalMood();
		if(results.size() > 0 && results != null) {
			jsonObject.setRows(results);
			jsonObject.setTotal(results.size());
		}
		return SUCCESS;
	}
	
	public String addOneMood() {
		session = ServletActionContext.getRequest().getSession();
		String co = (String) session.getAttribute("rand");
		if(!co.equals(code)) {
			jsonObject.setResult(1);
		}else {
			int result = moodBiz.addMood(mood);
			if(result > 0){
				jsonObject.setResult(2);
			} else {
				jsonObject.setResult(3);
			}
		}
		return SUCCESS;
	}

	public String moodByTag() {
		List<Mood> result = moodBiz.findMoodByTag(mood.getMtag());
		jsonObject.setRows(result);
		jsonObject.setTotal(result.size());
		return SUCCESS;
	}
	
	public String myMood() {
		List<Mood> result = moodBiz.findFriendMood(mood.getW_uid());
		jsonObject.setRows(result);
		jsonObject.setTotal(result.size());
		return SUCCESS;
	}
	
	public String seeOneMood() {
		session = ServletActionContext.getRequest().getSession();
		Mood m = moodBiz.findOneMood(mood.getMid());
		List<UserComment> cs = userCommentBiz.getOneObjComment(mood.getMid(), 4);
		List<UserReply> rs = userReplyBiz.getAllReply();
		if(m != null) {
			session.setAttribute(SessionAttributeName.ONEMOODDATA, m);
			session.setAttribute(SessionAttributeName.COMMENTDATA, cs);
			session.setAttribute(SessionAttributeName.REPLYDATA, rs);
			jsonObject.setResult(1);
		}
		return SUCCESS;
	}
	
	public String addComment() {
		int result = userCommentBiz.addUserComment(comment);
		jsonObject.setResult(result);
		return SUCCESS;
	}
	
	public String addReply() {
		int result = userReplyBiz.addUserReply(reply);
		jsonObject.setResult(result);
		return SUCCESS;
	}
	
	public String commentInfo() {
		int result = 0;
		session = ServletActionContext.getRequest().getSession();
		List<UserComment> comments = userCommentBiz.getOneObjComment(comment.getObjid(), 4);
		List<UserReply> replays = userReplyBiz.getAllReply();
		if(comments.size() > 0 && comments != null) {
			session.setAttribute(SessionAttributeName.COMMENTDATA, comments);
			result ++;
		}
		if(replays.size() > 0 && replays != null) {
			session.setAttribute(SessionAttributeName.REPLYDATA, replays);
			result ++;
		}
		jsonObject.setResult(result);
		return SUCCESS;
	}
	
	public JsonObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JsonObject jsonObject) {
		this.jsonObject = jsonObject;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public Mood getMood() {
		return mood;
	}

	public void setMood(Mood mood) {
		this.mood = mood;
	}

	public UserComment getComment() {
		return comment;
	}

	public void setComment(UserComment comment) {
		this.comment = comment;
	}

	public UserReply getReply() {
		return reply;
	}

	public void setReply(UserReply reply) {
		this.reply = reply;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
}
